import ENV_CONFIG from '@/env.js'
import {
	_cdn,
	_static,
	_css
} from '@/utils/index.js';
import {
	getToken
} from '@/utils/auth';
let timeout = null;
let methods = {
	/**
	 * 添加cdn域名前缀
	 */
	$cdn(url = '', cdnurl = '') {
		return _cdn(url, cdnurl)
	},
	/**
	 * 添加静态资源
	 */
	$static(url = '', staticurl = '') {
		return _static(url, staticurl)
	},
	/**
	 * 添加静态资源
	 */
	$css(url = '', staticurl = '') {
		return _css(url, staticurl)
	},
	/**
	 * 登录后跳转
	 * @param {Object} url 跳转地址
	 */
	$isLoginUrl(url) {
		if (this.$isLogin()) return
		uni.navigateTo({
			url
		})
	},
	/**
	 * 是否登录
	 * @returns Boolean
	 */
	$isLogin() {
		if (!getToken()) {
			uni.$u.toast('请先登录')
			if (timeout !== null) clearTimeout(timeout)
			timeout = setTimeout(() => {
				uni.navigateTo({
					url: '/pages/index/login'
				})
			}, 1000)
			return true
		}
		return false
	},
	$hrefs(url) {
		uni.navigateTo({
			url
		})
	},
	/**
	 * 页面回退
	 * @param delay 延时时间
	 */
	$back(delay = 0) {
		const pages = getCurrentPages();
		setTimeout(() => {
			if (pages.length > 1) {
				uni.navigateBack()
			} else {
				uni.switchTab({
					url: '/pages/index/index'
				})
			}
		}, delay)
	},
	/** 获取前页面栈实例 */
	$prePage() {
		let pages = getCurrentPages();
		let prePage = pages[pages.length - 2];
		// #ifdef H5
		return prePage;
		// #endif
		return prePage.$vm;
	},
	// 复制到剪贴板
	$copy(text, msg = '复制成功') {
		uni.setClipboardData({
			data: text,
			success: function() {
				uni.showToast({
					title: msg
				});
			}
		});
	},
	/**
	 * 时间戳转时间格式
	 * @param {Number} time - 时间戳
	 * @param {String} type - day= 年月日 second=年月日时分秒
	 * @param {String} interval - 拼接格式
	 */
	timeFormat(time, type, interval) {
		let int = interval || '-'
		var skr = new Date(time)
		let y = skr.getFullYear()
		let M = skr.getMonth() + 1
		let d = skr.getDate() < 10 ? '0' + skr.getDate() : skr.getDate()

		let h = skr.getHours() < 10 ? '0' + skr.getHours() : skr.getHours()
		let m = skr.getMinutes() < 10 ? '0' + skr.getMinutes() : skr.getMinutes()
		let s = skr.getSeconds() < 10 ? '0' + skr.getSeconds() : skr.getSeconds()

		if (type == 'day' || type == '') {
			return y + int + m + int + d
		}
		if (type == 'second') {
			return y + int + M + int + d + ' ' + h + ':' + m + ':' + s
		}
	},
	// 支付
	Payment(paydata, type) {
		// 微信小程序支付
		// #ifdef MP-WEIXIN
		var promise = new Promise((resolve, reject) => {
			uni.requestPayment({
				provider: 'wxpay',
				timeStamp: String(paydata.timeStamp),
				nonceStr: paydata.nonceStr,
				package: paydata.package,
				signType: paydata.signType,
				paySign: paydata.paySign,
				success: function(res) {
					// 支付成功
					resolve(res)
				},
				fail: function(err) {
					// 支付失败
					reject(err)
				}
			});
		})
		return promise
		// #endif
		// APP支付
		// #ifdef APP-PLUS
		var promise = new Promise((resolve, reject) => {
			uni.requestPayment({
				provider: type || 'wxpay', //wxpay alipay 支付方式
				orderInfo: paydata, //微信、支付宝订单数据 【注意微信的订单信息，键值应该全部是小写，不能采用驼峰命名】
				success: function(res) {
					resolve(res)
				},
				fail: function(err) {
					reject(res)
				}
			});
		})
		return promise
		// #endif
	},

	/**
	 * 打电话
	 * @param {String<Number>} phoneNumber - 数字字符串
	 */
	$callPhone(phoneNumber = '') {
		let num = phoneNumber.toString()
		uni.makePhoneCall({
			phoneNumber: num,
			fail(err) {
				console.log('makePhoneCall出错', err)
			},
		});
	},
	// 数组操作相关
	// 1.获取两个数组的不同元素
	getDiff(arr1, arr2) {
		return arr1.concat(arr2).filter((item, index, arr) =>
			arr.indexOf(item) === arr.lastIndexOf(item)
		)
	},
	// 2.获取两个数组的相同元素
	getSame(arr1, arr2) {
		return [...new Set(arr1)].filter(item =>
			arr2.includes(item)
		)
	}
}

const myFunctions = {
	install: function(Vue, options) {
		for (let key in methods) {
			Vue.prototype[key] = methods[key];
		}
	}
};
export default myFunctions